package cn.wgx.modules.sys.services;

import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysChinaCity;
import cn.wgx.modules.sys.entity.SysChinaCityExample;
import cn.wgx.modules.sys.entity.mapper.SysChinaCityMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 全国省份城市
 */

@Service
public class SysChinaCityService extends BaseService<SysChinaCity> {

    @Resource
    SysChinaCityMapper sysChinaCityMapper;

    public final static String CITY_PRE_KEY = "CHINA_CITY_LIST_";

    @Transactional(readOnly = true)
    @Cacheable(value = "sysCache" ,key = "#root.target.CITY_PRE_KEY + #parentid")
    public List<SysChinaCity> getList(Integer parentid) {
        SysChinaCityExample example = new SysChinaCityExample();
        SysChinaCityExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(parentid);
        List<SysChinaCity> list = sysChinaCityMapper.selectByExample(example);
        return list;
    }

    @Transactional(readOnly = true)
    @Cacheable(value = "sysCache" ,key = "#root.target.CITY_PRE_KEY + \"id_\" + #id")
    public SysChinaCity getOne(Integer id) {
        SysChinaCityExample example = new SysChinaCityExample();
        SysChinaCityExample.Criteria criteria = example.createCriteria();
        criteria.andCity_idEqualTo(id);
        List<SysChinaCity> list = sysChinaCityMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Transactional(readOnly = true)
    public List<SysChinaCity> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysChinaCityExample example = new SysChinaCityExample();
            SysChinaCityExample.Criteria criteria = example.createCriteria();
            criteria.andCity_idIn(uniList);
            List<SysChinaCity> eduCourseAssignmentHis = sysChinaCityMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }

}